package net.ocps.tchs.soap.entity;

import net.ocps.tchs.soap.Main;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;

public class Player extends Entity {

	/* we want a lot of integers */
	public Player( int... params ) {
		super( params );
	}

	public Player( float x, float y, int width, int height ) {
		super( x, y, width, height );
	}
	public Player( float x, float y, int width, int height, String image ) {
		super( x, y, width, height, image );
	}
	@Override
	public void render() {
		try {
			batch.begin();
			{
				if( direction == -1 ) {
					texture.setTexture( mLeft );
					direction = 0;
				} else if ( direction == 1 ) {
					texture.setTexture( mRight );
					direction = 0;
				}
				x = body.getPosition().x-31;
				y = body.getPosition().y-32;
				batch.setProjectionMatrix( Main.camera.combined );
				if( width <= 0 && height <= 0)
					batch.draw(
							texture, x, y, texture.getOriginX(), texture.getOriginY(), 
							texture.getWidth(), texture.getHeight(), texture.getScaleX(), 
							texture.getScaleY(), (float)Math.toDegrees( body.getAngle() ) );
				else if( width > 0 || height > 0 )
					batch.draw(
							texture, x, y, texture.getOriginX(), texture.getOriginY(), 
							width, height, texture.getScaleX(), texture.getScaleY(), 
							(float)Math.toDegrees( body.getAngle() ) );
				hitbox.set( x, y, texture.getWidth(), texture.getHeight() );
				batch.draw( texture, hitbox.x, hitbox.y, hitbox.width, hitbox.height );
			}
			batch.end();
		} catch( Exception e ) {
			if(batch != null)
			batch.end();
		}
	}

	@Override
	public void onCreate() {
		batch = new SpriteBatch();
		mLeft = new Texture(Gdx.files.internal("data/Soapguy LBase.gif"));
		mRight= new Texture(Gdx.files.internal("data/Soapguy RBase.gif"));
		texture = new Sprite( mLeft );
		bodyDef = new BodyDef();  
		bodyDef.type = BodyType.DynamicBody;  
		bodyDef.position.set(x, y);  
		body = Main.world.createBody(bodyDef);  
		body.setBullet( true );
		hits = new PolygonShape();  
		if( width <= 0 && height <= 0)
			hits.setAsBox( texture.getWidth()/2, texture.getHeight()/2 );
		else if( width > 0 || height > 0 )
			hits.setAsBox( width/2, height/2 );
		fixtureDef = new FixtureDef();  
		fixtureDef.shape = hits;  
		fixtureDef.density = 0.0f;  
		fixtureDef.friction = 0.80f;  
		fixtureDef.restitution = 0.0f;  
		body.createFixture(fixtureDef);  
	}

	public BodyDef bodyDef;
	public PolygonShape hits;
	public FixtureDef fixtureDef;
	public Texture mLeft, mRight;
	public int health = 3;
	public boolean dead = false;
}
